package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGrouponRules;
import org.linlinjava.litemall.db.mapper.LitemallGoodsDao;
import org.linlinjava.litemall.db.mapper.LitemallGrouponRulesDao;
import org.linlinjava.litemall.db.util.GrouponConstant;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;

@Service
public class LitemallGrouponRulesService {
    @Resource
	/* private LitemallGrouponRulesMapper mapper; */
    private LitemallGrouponRulesDao dao;
    @Resource
	/* private LitemallGoodsMapper goodsMapper; */
    private LitemallGoodsDao goodsDao;
    private LitemallGoods.Column[] goodsColumns = new LitemallGoods.Column[]{LitemallGoods.Column.id, LitemallGoods.Column.name, LitemallGoods.Column.brief, LitemallGoods.Column.picUrl, LitemallGoods.Column.counterPrice, LitemallGoods.Column.retailPrice};

    public int createRules(LitemallGrouponRules rules) {
        rules.setAddTime(LocalDateTime.now());
        rules.setUpdateTime(LocalDateTime.now());
        dao.insertTemplate(rules,true);
        return 1;
        //return mapper.insertSelective(rules);
    }

    /**
     * 根据ID查找对应团购项
     *
     * @param id
     * @return
     */
    public LitemallGrouponRules findById(Integer id) {
    	return dao.single(id);
        //return mapper.selectByPrimaryKey(id);
    }

    /**
     * 查询某个商品关联的团购规则
     *
     * @param goodsId
     * @return
     */
    public List<LitemallGrouponRules> queryByGoodsId(Integer goodsId) {
    	
    	LitemallGrouponRules rules=  new LitemallGrouponRules();
    	rules.setGoodsId(goodsId);
    	rules.setStatus(GrouponConstant.RULE_STATUS_ON);
    	rules.setDeleted(false);
    	return dao.template(rules);
		/*
		 * LitemallGrouponRulesExample example = new LitemallGrouponRulesExample();
		 * example.or().andGoodsIdEqualTo(goodsId).andStatusEqualTo(GrouponConstant.
		 * RULE_STATUS_ON).andDeletedEqualTo(false); return
		 * mapper.selectByExample(example);
		 */
    }

    public int countByGoodsId(Integer goodsId) {
    	
    	LitemallGrouponRules rules=  new LitemallGrouponRules();
    	rules.setGoodsId(goodsId);
    	rules.setStatus(GrouponConstant.RULE_STATUS_ON);
    	rules.setDeleted(false);
    	return (int)dao.templateCount(rules);
		/*
		 * LitemallGrouponRulesExample example = new LitemallGrouponRulesExample();
		 * example.or().andGoodsIdEqualTo(goodsId).andStatusEqualTo(GrouponConstant.
		 * RULE_STATUS_ON).andDeletedEqualTo(false); return
		 * (int)mapper.countByExample(example);
		 */
    }

    public List<LitemallGrouponRules> queryByStatus(Short status) {
    	
    	LitemallGrouponRules rules=  new LitemallGrouponRules();
    	rules.setStatus(GrouponConstant.RULE_STATUS_ON);
    	rules.setDeleted(false);
    	return dao.template(rules);
		/*
		 * LitemallGrouponRulesExample example = new LitemallGrouponRulesExample();
		 * example.or().andStatusEqualTo(status).andDeletedEqualTo(false); return
		 * mapper.selectByExample(example);
		 */
    }

    /**
     * 获取首页团购规则列表
     *
     * @param page
     * @param limit
     * @return
     */
    public List<LitemallGrouponRules> queryList(Integer page, Integer limit) {
        return queryList(page, limit, "add_time", "desc");
    }

    public List<LitemallGrouponRules> queryList(Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallGrouponRules> query = dao.createLambdaQuery();
    	query.andEq(LitemallGrouponRules::getStatus, GrouponConstant.RULE_STATUS_ON);
    	query.andEq(LitemallGrouponRules::getDeleted, false);
    	return query.select();
		/*
		 * LitemallGrouponRulesExample example = new LitemallGrouponRulesExample();
		 * example.or().andStatusEqualTo(GrouponConstant.RULE_STATUS_ON).
		 * andDeletedEqualTo(false); example.setOrderByClause(sort + " " + order);
		 * PageHelper.startPage(page, limit); return mapper.selectByExample(example);
		 */
    }

    /**
     * 判断某个团购规则是否已经过期
     *
     * @return
     */
    public boolean isExpired(LitemallGrouponRules rules) {
        return (rules == null || rules.getExpireTime().isBefore(LocalDateTime.now()));
    }

    /**
     * 获取团购规则列表
     *
     * @param goodsId
     * @param page
     * @param size
     * @param sort
     * @param order
     * @return
     */
    public List<LitemallGrouponRules> querySelective(String goodsId, Integer page, Integer size, String sort, String order) {
    	LambdaQuery<LitemallGrouponRules> query = dao.createLambdaQuery();
    	if (!StringUtils.isEmpty(goodsId)) {
    		query.andEq(LitemallGrouponRules::getGoodsId, Integer.parseInt(goodsId));
        }
    	query.andEq(LitemallGrouponRules::getDeleted, false);
    	
    	query.orderBy(sort + " " + order);
    	query.limit(page, size);
    	return query.select();
    	
    	
		/*
		 * LitemallGrouponRulesExample example = new LitemallGrouponRulesExample();
		 * example.setOrderByClause(sort + " " + order);
		 * 
		 * LitemallGrouponRulesExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(goodsId)) {
		 * criteria.andGoodsIdEqualTo(Integer.parseInt(goodsId)); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * PageHelper.startPage(page, size); return mapper.selectByExample(example);
		 */
    }

    public void delete(Integer id) {
    	
    	LitemallGrouponRules rules=  new LitemallGrouponRules();
    	rules.setDeleted(true);
    	rules.setId(id);
    	dao.updateTemplateById(rules);
        //mapper.logicalDeleteByPrimaryKey(id);
    }

    public int updateById(LitemallGrouponRules grouponRules) {
        grouponRules.setUpdateTime(LocalDateTime.now());
        return dao.updateTemplateById(grouponRules);
        //return mapper.updateByPrimaryKeySelective(grouponRules);
    }
}